package com.cmdi.opensite.controller;

import com.cmdi.opensite.entity.ActivityCategory;
import com.cmdi.opensite.service.ActivityCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动分类控制器
 */
@RestController
@RequestMapping("/api/categories")
public class ActivityCategoryController {
    
    @Autowired
    private ActivityCategoryService activityCategoryService;
    
    /**
     * 获取所有活动分类
     */
    @GetMapping
    public ResponseEntity<?> getAllCategories() {
        List<ActivityCategory> categories = activityCategoryService.getAllCategories();
        return ResponseEntity.ok(categories);
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getCategoryById(@PathVariable Long id) {
        ActivityCategory category = activityCategoryService.getCategoryById(id);
        if (category != null) {
            return ResponseEntity.ok(category);
        }
        return ResponseEntity.notFound().build();
    }
    
    /**
     * 添加分类（管理员接口）
     */
    @PostMapping
    public ResponseEntity<?> addCategory(@RequestBody ActivityCategory category) {
        boolean success = activityCategoryService.saveCategory(category);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "添加成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "添加失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 更新分类（管理员接口）
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateCategory(@PathVariable Long id, @RequestBody ActivityCategory category) {
        category.setId(id);
        boolean success = activityCategoryService.saveCategory(category);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "更新成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "更新失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 删除分类（管理员接口）
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteCategory(@PathVariable Long id) {
        boolean success = activityCategoryService.deleteCategory(id);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "删除成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "删除失败，该分类可能已被使用");
        return ResponseEntity.badRequest().body(result);
    }
}